package dst.ass1.jpa.validator;

import java.util.logging.Logger;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class WorkUnitCapacityValidator implements ConstraintValidator<WorkUnitCapacity, Integer> {  //extends from ConstraintValidator
	
    private int maxWorkUnits;
    private int minWorkUnits;

    private final static Logger LOGGER = Logger.getLogger(WorkUnitCapacityValidator.class.getName());
    
    @Override
    public void initialize(WorkUnitCapacity constraint) { //initializes the annotation
    	minWorkUnits = constraint.min();
        maxWorkUnits = constraint.max();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintContext) {  
    	boolean valid=true;
    	
    	if (value == null)  //proof that is is not null
    		valid=false;

    	  int numCpus = value.intValue();
    	  if (numCpus < this.minWorkUnits || numCpus > this.maxWorkUnits)  //must be between limitation
    		  valid=false;  //not valid

    	  if(!valid){
    		  LOGGER.info("Number of WorkUnits is not valid!");
    	  }
    	  
    	  return valid;  //valid
    }

}